package com.baofu.paycore.manager.storage.impl;

import com.baofu.paycore.common.utils.Validate;
import com.baofu.paycore.dal.mapper.TransferMapper;
import com.baofu.paycore.dal.model.TransferDO;
import com.baofu.paycore.manager.storage.Storage;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.service.facade.model.PaycorePageDTO;
import com.system.commons.exception.BizServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * description:转账单操作
 *
 * <p>
 *      1.新增转账单
 *      2.更新转账单
 *      3.根据id获取转账订单
 *      4.根据支付号获取转账订单
 *      5.分页查询转账单
 *      6.获取转账单数量
 * </p>
 * User: liu ting  Date: 2016/5/19 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Repository
public class TransferManager implements Storage<TransferDO> {

    /**
     * 转账单mapper
     */
    @Autowired
    private TransferMapper transferMapper;


    /**
     * 1.新增转账单
     *
     * @param transferDO 转账单
     */
    @Override
    public int addOrder(TransferDO transferDO){

        int count = transferMapper.insert(transferDO);
        Validate.checkUpdate(count);
        return count;
    }

    /**
     * 2.更新转账单
     *
     * @param transferDO 转账单
     */
    @Override
    public int modifyOrder(TransferDO transferDO) {

        int count = transferMapper.update(transferDO);
        Validate.checkUpdate(count, PayCoreErrorCode.UPDATE_DATA_NOT_ONE.getErrorDesc());
        return count;
    }

    /**
     * 3.根据id获取转账订单
     *
     * @param id 主键id
     * @return 转账单
     */
    public TransferDO getById(Long id){

        TransferDO transferDO = transferMapper.getById(id);
        if(transferDO == null){
            log.error("根据id获取转账订单异常，查询内容不存在");
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }

        return transferDO;
    }

    /**
     * 4.根据支付号获取转账订单
     *
     * @param payNo 支付号
     * @return 转账单
     */
    public TransferDO getByPayNo(String payNo){
        TransferDO transferDO = transferMapper.selectByPayNo(payNo);
        if(transferDO == null){
            log.error("根据支付号获取转账订单异常，查询内容不存在");
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }

        return transferDO;
    }

    /**
     * 5.分页查询转账单
     *
     * @param transferDO 转账单
     * @return 转账单
     */
    public List<TransferDO> selectTransferPage(TransferDO transferDO, PaycorePageDTO paycorePageDTO){

        return transferMapper.selectTransferByPage(transferDO,paycorePageDTO.getStartDate(),paycorePageDTO.getEndDate(), paycorePageDTO.getStartRow(), paycorePageDTO.getPageCount());
    }

    /**
     * 6.获取转账单数量
     *
     * @param transferDO 转账单
     * @return 数量
     */
    public int selectTransferCount(TransferDO transferDO,PaycorePageDTO paycorePageDTO){

        return transferMapper.selectTransferCount(transferDO,paycorePageDTO.getStartDate(),paycorePageDTO.getEndDate());
    }

    /**
     * 废弃订单
     *
     * @param transferDO 订单信息
     */
    @Override
    public int deleteOrder(TransferDO transferDO) {
        return 0;
    }

    /**
     * 根据支付号获取
     *
     * @param requestOrderNo 请求订单号
     * @param requestBizNo   请求流水号
     * @return 转账单
     */
    public TransferDO selectByOrderNo(String requestOrderNo, String requestBizNo, String requestSystem) {

        return transferMapper.selectByOrderNo(requestOrderNo,requestBizNo,requestSystem);
    }

    /**
     * 根据订单号、支付流水号查询交易
     *
     * @return 交易
     */
    public TransferDO selectByOrderNoAndBizNo(String requestOrderNo, String requestBizNo, String requestSystem,
                                              Date requestDate){
        TransferDO transferDO = transferMapper.selectByOrderNoAndBizNo(
                requestOrderNo, requestBizNo, requestSystem, requestDate);
        if (transferDO == null) {
            log.error("查询结果集为空:{} {} {} {}", requestOrderNo, requestBizNo, requestSystem, requestDate);
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }
        return transferDO;
    }
}
